import pygtk
pygtk.require('2.0')
import gtk

class KeyBoard(gtk.VBox):

    def insertDigit(self, widget, value, entryWidget):
        entryWidget.set_text(entryWidget.get_text() + value)
        
    def backspace(self, widget, entryWidget):

        if len(entryWidget.get_text()) != 0:
            lastCharIdx = len(entryWidget.get_text()) - 1
            print lastCharIdx
            entryWidget.set_text(entryWidget.get_text()[0:lastCharIdx])
    
    def build(self, keyPadMode, entryWidget):
        
        hVirtualKeyboardBox1 = gtk.HButtonBox()
        hVirtualKeyboardBox1.set_layout(gtk.BUTTONBOX_SPREAD)
        hVirtualKeyboardBox1.set_spacing(5)
        hVirtualKeyboardBox1.show()
        
        hVirtualKeyboardBox2 = gtk.HButtonBox()
        hVirtualKeyboardBox2.set_layout(gtk.BUTTONBOX_SPREAD)
        hVirtualKeyboardBox2.set_spacing(5)
        hVirtualKeyboardBox2.show()
        
        self.btVirtualKeyboard = []
        
        # we can put immediately on vbox
        self.pack_start(hVirtualKeyboardBox1, expand=True, fill=True, padding=5)
        self.pack_start(hVirtualKeyboardBox2, expand=True, fill=True, padding=5)
        
        range1 = 5
        range2 = 10
        
        if keyPadMode == "3":
            hVirtualKeyboardBox3 = gtk.HButtonBox()    
            hVirtualKeyboardBox3.set_layout(gtk.BUTTONBOX_SPREAD)
            hVirtualKeyboardBox3.set_spacing(5)
            hVirtualKeyboardBox3.show()
            self.pack_start(hVirtualKeyboardBox3, expand=True, fill=True, padding=5)
            range1 = 4
            range2 = 8
                   
        # build virtual keyboard
        for x in range (0,range1):
            self.btVirtualKeyboard.append(gtk.Button(str(x)))
            self.btVirtualKeyboard[x].show()
            hVirtualKeyboardBox1.pack_start(self.btVirtualKeyboard[x], True, True,5)
            self.btVirtualKeyboard[x].connect("clicked", self.insertDigit, str(x), entryWidget)
        
        for x in range (range1,range2):
            self.btVirtualKeyboard.append(gtk.Button(str(x)))
            self.btVirtualKeyboard[x].show()
            hVirtualKeyboardBox2.pack_start(self.btVirtualKeyboard[x], True, True,5)
            self.btVirtualKeyboard[x].connect("clicked", self.insertDigit, str(x), entryWidget)
        
        if keyPadMode == "3":
            for x in range (8,10):
                self.btVirtualKeyboard.append(gtk.Button(str(x)))
                self.btVirtualKeyboard[x].show()
                hVirtualKeyboardBox3.pack_start(self.btVirtualKeyboard[x], True, True,5)
                self.btVirtualKeyboard[x].connect("clicked", self.insertDigit, str(x), entryWidget)
                
            # adjust <- and .
            self.btVirtualKeyboard.append(gtk.Button("<-"))
            self.btVirtualKeyboard.append(gtk.Button("."))
            hVirtualKeyboardBox3.pack_start(self.btVirtualKeyboard[10], True, True,5)
            hVirtualKeyboardBox3.pack_start(self.btVirtualKeyboard[11], True, True,5)
            
        else:
            # adjust <- and .
            self.btVirtualKeyboard.append(gtk.Button("<-"))
            self.btVirtualKeyboard.append(gtk.Button("."))
            hVirtualKeyboardBox1.pack_start(self.btVirtualKeyboard[10], True, True,5)
            hVirtualKeyboardBox2.pack_start(self.btVirtualKeyboard[11], True, True,5)
            
        self.btVirtualKeyboard[10].show()
        self.btVirtualKeyboard[10].connect("clicked", self.backspace, entryWidget)
        self.btVirtualKeyboard[11].show()
        self.btVirtualKeyboard[11].connect("clicked", self.insertDigit, ".", entryWidget)
        
        
    def __init__(self, keyPadMode, entryWidget ):
        
        gtk.VBox.__init__(self, False, int(keyPadMode))
        
        self.build(keyPadMode, entryWidget)
        self.show()